package com.ysd.salepromotion.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ysd.common.entity.Product;
import com.ysd.salepromotion.common.JSONResult;
import com.ysd.salepromotion.common.ResultList;
import com.ysd.salepromotion.entity.Active;
import com.ysd.salepromotion.entity.ActiveAllInfo;
import com.ysd.salepromotion.entity.ActiveResutInfo;
import com.ysd.salepromotion.entity.MSProduct;
import com.ysd.salepromotion.entity.Rule;
import com.ysd.salepromotion.mapper.ActiveMapper;
import com.ysd.salepromotion.mapper.ActiveRuleMapper;
import com.ysd.salepromotion.param.in.AddActiveRuleRequestType;
import com.ysd.salepromotion.param.in.QueryAllActiveRequestType;
import com.ysd.salepromotion.service.SalePromotionService;
import com.ysd.salepromotion.util.JsonUtils;

@Service
public class SalePromotionServiceImpl implements SalePromotionService {
	
	@Autowired
	private ActiveMapper activeMapper;
	
	@Autowired
	private ActiveRuleMapper activeRuleMapper;

	@Override
	public JSONResult addActiveRule(AddActiveRuleRequestType request) {
		JSONResult result = JSONResult.build(200, "成功", null);
		
		generateDes(request);//生成活动描述信息
		//插入活动信息
		Active active = JsonUtils.copyObject(request, Active.class);
		
		activeMapper.insert(active);
		//插入活动规则信息
		//获取活动的主键
		long activeId = active.getId();
		request.getRuleList().stream().forEach(rule ->{
			rule.setActiveId(activeId);
		});
		activeRuleMapper.insertBatch(request.getRuleList());
		return result;
	}

	//生成活动描述信息
	private void generateDes(AddActiveRuleRequestType active) {
		String des = "无效活动";
		//判断是什么活动
		if(active.getActiveType() == 1) { // 1:满减 
			switch (active.getPromotionType()) {
			case 11: // 11:阶梯满减
				des = String.format("满%s减%s",active.getRuleList().get(0).getLevelMoney(),active.getRuleList().get(0).getSubtractMoney());
				active.setDescribe(des);
				break;
			case 12: // 12:每满减
				des = String.format("满%s减%s",active.getRuleList().get(0).getLevelMoney(),active.getRuleList().get(0).getSubtractMoney());
				active.setDescribe(des);
				break;
			default:
				active.setDescribe(des);
				break;
			}
		}else if(active.getActiveType() == 2) {//  2:满折
			switch (active.getPromotionType()) {
			case 21: // 21:满额折 
				des = String.format("%d件%d折",active.getRuleList().get(0).getLevelNum(),active.getRuleList().get(0).getDiscount());
				active.setDescribe(des);
				break;
			case 22: // 22满件折
				des = String.format("%d件%d折",active.getRuleList().get(0).getLevelNum(),active.getRuleList().get(0).getDiscount());
				active.setDescribe(des);
				break;
			default:
				active.setDescribe(des);
				break;
			}
		}else if(active.getActiveType() == 3) {
			active.setDescribe("秒杀");
		}else {
			active.setDescribe(des);
		}
	}

	/**
	 * 下架活动
	 */
	@Override
	public JSONResult updateActive(AddActiveRuleRequestType request) {
		//将产品表中该活动信息全部删除
		//TODO 调用产品中心接口
		//下架本活动
		Active active = JsonUtils.copyObject(request, Active.class);
		activeMapper.updateActive(active);
		return null;
	}

	@Override
	public ResultList queryAllActives(QueryAllActiveRequestType request) {
		ResultList result = ResultList.toClient(0, "success", 100, null);
		try {
			request.setPage((request.getPage()-1)*request.getLimit());
			List<Active> activeList = activeMapper.queryActiveList(request);
			result.setData(activeList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public ResultList queryAllActivesGroup(HttpServletRequest request) {
		ResultList result = ResultList.toClient(0, "success", 100, null);
		try {
			List<ActiveAllInfo> activeList = activeMapper.queryAllActivesGroup();
			List<ActiveResutInfo> resultActiveList = new ArrayList<>();
			activeList.stream().forEach(item -> {
				if(item.getActiveType() == 1) {
					item.setActiveTypeName("MEJ");
				}else if(item.getActiveType() == 2) {
					item.setActiveTypeName("MJZ");
				}else if(item.getActiveType() == 3) {
					item.setActiveTypeName("MS");
				}
				ActiveResutInfo ar = JsonUtils.copyObject(item, ActiveResutInfo.class);
				Rule r = JsonUtils.copyObject(item, Rule.class);
				ar.setRule(JsonUtils.toJSONString(r).replace("/", ""));
				resultActiveList.add(ar);
			});
			Map<String, List<ActiveResutInfo>> groupByActive = resultActiveList.stream().collect(Collectors.groupingBy(ActiveResutInfo::getActiveTypeName));
			 //遍历分组
			result.setData(groupByActive);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public ResultList delActive(long aId) {
		ResultList resultList = ResultList.toClient(0, "success", 0, null);
		try {
			activeMapper.delActive(aId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}

	/**
	 * 添加秒杀产品
	 */
	@Override
	public ResultList addMsProductList(List<Product> request) {
		ResultList result = ResultList.toClient(0,"success", 0, null);
		try {
			for(Product p : request) {
				MSProduct ms = new MSProduct();
				ms = JsonUtils.copyObject(p, MSProduct.class);
				ms.setStart(p.getPcname());
				ms.setEnd(p.getRule());
				ms.setMsprice(p.getOldprice());
				activeMapper.addProductList(ms);
			}
		} catch (Exception e) {
			result.setCode(-1);
			result.setMsg(e.toString());
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public ResultList queryProListByAId(String activeId) {
		ResultList result = ResultList.toClient(0, "success", 1000, null);
		try {
			List<MSProduct> list = activeMapper.queryProListByAId(activeId);
			result.setData(list);
		} catch (Exception e) {
			result.setCode(-1);
			result.setMsg(e.toString());
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public ResultList queryMSProList() {
		ResultList result = ResultList.toClient(0, "success", 1000, null);
		try {
			List<MSProduct> list = activeMapper.queryMSProList();
			result.setData(list);
		} catch (Exception e) {
			result.setCode(-1);
			result.setMsg(e.toString());
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public ResultList delMSProList(long msPid) {
		ResultList result = ResultList.toClient(0, "success", 0, null);
		try {
			int num = 0;//activeMapper.delMSProList(msPid);
			if(num == 0) {
				result.setCode(-1);
				result.setMsg("删除失败!");
			}
		} catch (Exception e) {
			// TODO: handle exception
			result.setCode(-1);
			result.setMsg("接口异常!");
		}
		
		return result;
	}

}
